function [gg] = avg_inf_6to10(A,bt,pi_bar,pi_lim)

% given today, what are the probabilities in 5 year
g = (A(bt,:)*A^(5))';

% What infaltion is actually in each bin
pi = pi_bar;

% number of bins
s = size(pi_bar,1);

% bin index
bin = (1:s)';

tol = 1e-7;


% For year 6 to 10
%for i = 7:10
 for i=2:5   
    
    % Calcualtes average inflation up to now, by using previous avg
    % inflation and current inflation
    
    %pi = pi*(i-1)/i + pi_bar'/i;
    pi = pi*(i-1)/(i) + pi_bar'/(i);
    
    % Probability distribution at time t, for each initial bin, so a matrix 
    g = g.*A(bin,:);
    
    % repeat 1 to 8, 8 times
    bin = repmat((1:s),size(g,1),1);
    
    % Vectorize 
    pi = pi(:); bin = bin(:); g = g(:);
    
    %Average infaltion and the last bin 
    d = [pi bin];
    
    % Collapse differnet pathes that result in the same inflation bin
    [d,~,ic] = uniquetol(d,tol,'ByRows',true);
    
    dr = size(d,1);
    d = [d -ones(dr,1)]; %#ok<AGROW>
%    d(:,3) = sum(repmat(g',dr,1).*(repmat(ic',dr,1)==repmat((1:dr)',1,size(g,1))),2); % loop below = this line, loop is faster
    for j = 1:dr
        d(j,3) = sum(g(ic==j,1));
    end
    d(d(:,3)==0,:) = [];
    pi = d(:,1); bin = d(:,2); g = d(:,3);
end

bin = (pi_lim'-pi>-tol); bin  = s+1 - sum(bin,2);
gg = zeros(s,1);
for i = 1:s
    
    % Probability of average infaltion being in each bin
    gg(i,1) = sum(g(bin==i,1));
end
